package com.itheima.controller;

import com.itheima.pojo.Clazz;
import com.itheima.pojo.ClazzPageRequest;
import com.itheima.pojo.PageResponseResult;
import com.itheima.pojo.Result;
import com.itheima.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/clazzs")
public class ClazzController {
    @Autowired
    private ClazzService clazzService;

    //分页条件查询所有班级
    @GetMapping
    public Result queryPageClazz(ClazzPageRequest clazzPageRequest) {
        log.info("查询请求参数为:{}",clazzPageRequest);
        PageResponseResult<Clazz> pageClazz = clazzService.queryPageClazz(clazzPageRequest);
        return Result.success(pageClazz);
    }

    //新增班级
    @PostMapping
    public Result insertClazz(@RequestBody Clazz clazz) {
        log.info("请求体为: {}", clazz.toString());
        clazzService.insertClazz(clazz);
        return Result.success();
    }

    //修改班级,1.根据id查询班级
    @GetMapping("/{id}")
    public Result queryByIdClazz(@PathVariable Integer id) {
        log.info("要查询的班级的id:{}",id);
        Clazz clazz = clazzService.queryByIdClazz(id);
        return Result.success(clazz);
    }

    //修改班级,2.修改信息
    @PutMapping
    public Result updateClazz(@RequestBody Clazz clazz) {
        log.info("clazz修改成为: {}" , clazz);
        clazzService.updateClazz(clazz);
        return Result.success();
    }

    //删除班级
    @DeleteMapping("/{id}")
    public Result deleteClazz(@PathVariable Integer id) {
        log.info("要删除的班级的id:{}",id);
        clazzService.deleteClazz(id);
        return Result.success();
    }

    //查询全部班级
    @GetMapping("/list")
    public Result queryClazzs() {
        List<Clazz> clazzList = clazzService.queryClazzs();
        return Result.success(clazzList);
    }


}
